﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;
using TestForFulentApi.Models;

namespace TestForFulentApi.EntityFramework
{
    public class EfContext : DbContext
    {
        public EfContext() : base("conn")
        {

        }

        public DbSet<Article> Articles { get; set; }

        public DbSet<Blog> Blogs { get; set; }

        public DbSet<Author> Authors { get; set; }

        public DbSet<Address> Addresses { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Author>().HasKey(c => c.Id);
           modelBuilder.Entity<Address>().HasKey(c => c.Id);

            modelBuilder.Entity<Author>().HasRequired(c => c.Address).WithRequiredPrincipal(c=>c.Author).Map( c=>c.MapKey( "AuthorId")).WillCascadeOnDelete(false );

            modelBuilder.Entity<Article>()
                .HasRequired(c => c.Author)
                .WithMany(c => c.Articles)
                .HasForeignKey(c => c.AuthorId).WillCascadeOnDelete(false);

            modelBuilder.Entity<Blog>().HasMany(c => c.Authors).WithMany(c => c.Blogs).Map( c=>c.ToTable( "Blogs_Authors"));

            // modelBuilder.Entity<Author>().HasOptional(c => c.Address).WithOptionalPrincipal(c => c.Author).Map( c=>c.MapKey( "AuthorId")).WillCascadeOnDelete(false );
            //  base.OnModelCreating(modelBuilder);




        }
    }
}